package com.yami.shop.listener;

import com.yami.shop.bean.enums.EsOperationType;
import com.yami.shop.bean.event.EsProductUpdateEvent;
import com.yami.shop.bean.event.PaySuccessOrderEvent;
import com.yami.shop.bean.event.PaySuccessScoreOrderEvent;
import com.yami.shop.bean.model.OrderItem;
import com.yami.shop.bean.order.PaySuccessOrderOrder;
import com.yami.shop.bean.vo.EsProdUpdateVO;
import com.yami.shop.common.config.Constant;
import com.yami.shop.dao.ProdExtensionMapper;
import com.yami.shop.service.ShopWalletService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 支付成功回调
 *
 * @author LGH
 */
@Component("defaultPaySuccessListener")
@AllArgsConstructor
@Slf4j
public class PaySuccessOrderListener {

    private final ShopWalletService shopWalletService;
    private final ProdExtensionMapper prodExtensionMapper;
    private final ApplicationEventPublisher eventPublisher;

    @EventListener(PaySuccessOrderEvent.class)
    @Order(PaySuccessOrderOrder.SHOP_BILLING)
    public void shopPaySuccessListener(PaySuccessOrderEvent event) {
        List<com.yami.shop.bean.model.Order> orders = event.getOrders();
        // 如果是积分订单,直接退出
        if(Objects.equals(orders.get(0).getShopId(),Constant.PLATFORM_SHOP_ID)){
            return;
        }
        // 进行分账，添加一个分账记录表，因为每一个订单进行分完帐后要进行退款，那么退款的时候也应该按照下单的时候的分账比例进行原路返回
        shopWalletService.addAmountByOrder(orders,event.getPayNo());
        // 更新商品销量
        // prodExtensionMapper.updateSoldNum(event.getAllOrderItems());
        // 更新es中的商品销量
        try {
            // 获取商品的销量
            Map<Long, EsProdUpdateVO> map = new HashMap<>();
            for (com.yami.shop.bean.model.Order order : orders) {
                for (OrderItem orderItem : order.getOrderItems()) {
                    EsProdUpdateVO esProdUpdateVO = map.get(orderItem.getProdId());
                    if (Objects.isNull(esProdUpdateVO)) {
                        esProdUpdateVO = new EsProdUpdateVO();
                        esProdUpdateVO.setProdId(orderItem.getProdId());
                        // 增加销量
                        esProdUpdateVO.setType(1);
                        esProdUpdateVO.setCount(0);
                        map.put(orderItem.getProdId(), esProdUpdateVO);
                    }
                    esProdUpdateVO.setCount(esProdUpdateVO.getCount() + orderItem.getProdCount());
                }
            }
            eventPublisher.publishEvent(new EsProductUpdateEvent(new ArrayList<>(map.values()), EsOperationType.UPDATE_ORDER_SOLD_NUM_BATCH));
        } catch (Exception e) {
            log.error("订单支付成功-更新es商品销量失败，错误为：{}", e);
        }
    }

    @EventListener(PaySuccessScoreOrderEvent.class)
    @Order(PaySuccessOrderOrder.SHOP_BILLING)
    public void shopPaySuccessListener(PaySuccessScoreOrderEvent event) {
        com.yami.shop.bean.model.Order order = event.getOrder();
        // 进行平台分账，添加一个分账记录表
        shopWalletService.addAmountByScoreOrder(order);
    }
}
